//
//  BookMarkList.swift
//  test
//
//  Created by  mahoPunch on 2021/6/10.
//

import SwiftUI

struct BookmarkList: View {
    
    //获取bookmark单元的闭包模板
    typealias onBookmarkIDFetch = (_ bookmarkID: UUID) -> Void
    typealias onBookmarkFetch = (_ bookmark: Bookmark) -> Void
    typealias onYRNKBookmarkFetch = (_ yrnkbookmark: YRNKBookmark) -> Void
    
    
    //@EnvironmentObject var modelData: BookmarkData
    //@Binding var isEditing: Bool
    
    
    var bookmarks:[YRNKBookmark]
    
    @Binding var isSelectMode: Bool
    @Binding var isMoveMode: Bool
    @Binding var bookmarkCache: YRNKBookmark
    @Binding var selectedList:Set<YRNKBookmark>
    
    //从BookmarkView传进来的回调函数
    var onSummonEditAlert: onBookmarkFetch?
    var onBookmarkDelete: onBookmarkIDFetch?
    var onFolderClick: onBookmarkIDFetch?
    var onBookmarkClick: onBookmarkIDFetch?
    
    
    var callback:ViewCallBack?
    
    
    @State var isSelected = false
    
    
    
    func clickFolderBookmark(_ bookmarkID: UUID) -> Void{
        onFolderClick?(bookmarkID)
    }
    
    var body: some View {
        
        ScrollView {
            ForEach(bookmarks, id: \.self.this){
                bookmarkCell in
                if (!isMoveMode || ((bookmarkCache.this.id != bookmarkCell.this.id) && bookmarkCell.isDirectory)){
                    BookmarkRow(bookmark: bookmarkCell.this, isDirectory: bookmarkCell.isDirectory, canRowBeSelected: $isSelectMode, isMoveMode: $isMoveMode, isSelected: selectedList.contains(bookmarkCell), onEditClickClosure: {
                        onSummonEditAlert?(bookmarkCell.this)
                    }, onDeleteClickClosure: {
                        IDOfBookmarkToBeDelete in
                        onBookmarkDelete?(IDOfBookmarkToBeDelete)
                        
                    }, onBookmarkMoveClosure: {
                        bookmarkCache = bookmarkCell
                        isMoveMode = true
                    })
                    .onTapGesture {
                        if isSelectMode{//选中模式
                            if selectedList.contains(bookmarkCell){
                                //取消选择
                                selectedList.remove(bookmarkCell)
                            }
                            else{//选择
                                selectedList.insert(bookmarkCell)
                            }
                        }
                        else {//点击书签卡片
                            if bookmarkCell.isDirectory {
                                //是目录，更新至子文件夹
                                onFolderClick?(bookmarkCell.this.id)
                            }
                            else {
                                //是书签，跳转
                                onBookmarkClick?(bookmarkCell.this.id)
                            }
                            
                        }
                    }/* onTapGesture */
                    .onLongPressGesture {
                        if(!isSelectMode){
                            selectedList.removeAll()
                            isSelectMode = true
                        }
                        
                    }
                    .contentShape(Rectangle())
                    //Text(bookmarkCell.parent!.this.name)
                }
                
                
            }
            
        }
        .padding(.horizontal, 5)
            //.padding(.top, 5)
        
        
        
//        ScrollView {
//            ForEach(modelData.bookmarks) {
//                bookmarkCell in
//                //给每行绑定一个跳转，去往相应url页面
//                //再给每行传入一个回调，row中edit和delete调用
//
//                BookmarkRow(bookmark: bookmarkCell)
//                    .onTapGesture {
//                        //访问指定页面
//                    }
//                    .onTapGesture {
//                        callback?.goToView(by: "webview(暂定)", sender: self, with: ["siteName":bookmark.siteName,"url":bookmark.url])
//
//                    }
//            }
//        }.padding(.horizontal,7)
        
    }
}

//struct BookmarkList_Previews: PreviewProvider {
//
//    static var previews: some View {
//        BookmarkList(bookmarks: bookmarks)
//            
//    }
//}
